package com.example.demo.util;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/7/28.
 */
public class DateUtil {

    /**
     *
     * @param i      正往后 负往前
     * @param month  1 月份加减 i 个月
     * @param date  日期字符串
     * @param type  日期格式
     * @return
     */
    public static String getTime(int i, int month, String date,String type) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        Date dateto = null;
        try {
            dateto = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(dateto);

        if (month == 0) {
            c.add(Calendar.DAY_OF_MONTH, i);
        } else if (month == 1) {
            c.add(Calendar.MONTH, i);
        }

        String mDateTime = sdf.format(c.getTime());
//        System.out.println(mDateTime);
        return mDateTime;
//        String strStart = mDateTime.substring(0);//2007-10-29 09:30
//        System.out.println(strStart);
    }

    /**
     * 日期格式转换，解决MySQL日期查询转String后带.0的问题
     *
     * @param dateTime
     * @return
     */
    public static String formatDate(String dateTime) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateTime;
        try {
            if (StringUtils.isNotBlank(dateTime)) {
                time = format.format(format.parse(dateTime));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /*
     * 将时间转换为时间戳
     * type 转换的时间格式
     */
    public static long dateToStamp(String s,String type) throws ParseException {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime() / 1000;
        return ts;
    }

    /**
     * 时间戳转换成String
     * @param s
     * @param type 转换的时间格式 yyyy-MM-dd HH:mm:ss
     * @return
     * @throws ParseException
     */
    public static String stampTodate(Long s,String type) throws ParseException {
        //时间戳转化为Sting或Date
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat(type);
        String d = format.format(s * 1000);
        return d;
    }

    /**
     * 获取当前的时间
     * @param type 格式yyyy-MM-dd
     * @return
     */
    public static String getNowDate(String type) {
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat dateFormat = new SimpleDateFormat(type);
        String nowTime = dateFormat.format(new Date());
        return nowTime;
    }

    /**
     * 两个时间之间相差距离多少天
     *
     * @param str1 时间参数 1：
     * @param str2 时间参数 2：
     * @return 相差天数
     */
    public static long getDistanceDays(String str1, String str2) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days + 1;
    }

    public static String percentage(Double date) {
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        percentFormat.setMaximumFractionDigits(2); //最大小数位数
//            percentFormat.setMaximumIntegerDigits(3);//最大整数位数
        percentFormat.setMinimumFractionDigits(2); //最小小数位数
//            percentFormat.setMinimumIntegerDigits(1);//最小整数位数
        return percentFormat.format(date);
    }

    /**
     * 两个时间之间相差距离多少小时
     *
     * @param str1 时间参数 1：
     * @param str2 时间参数 2：
     * @return 相差小时数
     */
    public static long getDistanceHours(String str1, String str2) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd-hh");
        Date one;
        Date two;
        long hours = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            hours = diff / (1000 * 60 * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hours + 1;
    }


    //获取距离dateTime多少天的时间，前+ ,后-
    public static String getDay(String dateTime, int i) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = df.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = date.getTime() - i * 3600 * 24 * 1000;
        return df.format(new Date(time));
    }

    /*
    * 获取指定月份的最后一天
    * 格式yyyy-MM-dd
    * */
    public static String getMonthLastDay(String date) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 获取Calendar
        Calendar calendar = Calendar.getInstance();
        // 设置时间,当前时间不用设置
        calendar.setTime(dateFormat.parse(date));
        // 设置日期为本月最大日期
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));

        // 打印
        String monthLastDay = dateFormat.format(calendar.getTime());
        return monthLastDay;
    }

    //世界时间转换
    public static String wordtime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //Date转字符串
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = addDateMinut(sdf.format(date),8*60);
        return result;
    }

    //在传入时间day上增加x分钟
    public static String addDateMinut(String day, int x)//返回的是字符串型的时间，输入的
    //是String day, int x
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
        //引号里面个格式也可以是 HH:mm:ss或者HH:mm等等，很随意的，不过在主函数调用时，要和输入的变
        //量day格式一致
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";
//        System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, x);// 24小时制
        date = cal.getTime();
//        System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return format.format(date);

    }


    public static long dateStartTime(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(s);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }


    public static void main(String[] args) {
        String date = DateUtil.getNowDate("yyyy-MM-dd");
        String []datas = date.split("-");
        String time = datas[0]+"-"+datas[1];
        for(int i=1;i<4;i++){
            String timedate = DateUtil.getTime(-i,1,date,"yyyy-MM");
            System.out.println(timedate);
        }
    }
}
